<template>
  <view class="page">
    <!--		还未领取过-->
    <unReceive
      v-if="!hasReceiveResource"
      :config="config"
      :get-resource-type-config="getResourceTypeConfig"
      :hasReceiveResource.sync="hasReceiveResource"
      @receive="
        () => {
          hasReceiveResource.link && openCopyLinkPopup(hasReceiveResource.link)
        }
      "
    ></unReceive>
    <!--		已经领取过-->
    <has-receive
      :config="config"
      v-else
      :get-resource-type-config="getResourceTypeConfig"
      :hasReceiveResource.sync="hasReceiveResource"
      @copyLink="
        (link) => {
          openCopyLinkPopup(link)
        }
      "
    ></has-receive>
    <!--		复制链接弹窗-->
    <copyLinkPopup ref="copyLinkPopup"></copyLinkPopup>
  </view>
</template>

<script>
import { getNginxPageConfigUrl, getShowNginxPicUrl } from "@/utils/settings"
import request from "@/utils/request"
import { showToast } from "@/utils"
import unReceive from "./components/unReceive.vue"
import persistence from "@/store/persistence"
import urlUtils from "@/utils/urlUtils"
import copyLinkPopup from "./components/copyLinkPopup.vue"
import hasReceive from "./components/hasReceive.vue"

export default {
  components: { unReceive, copyLinkPopup, hasReceive },
  methods: {
    getResourceTypeConfig(type, config) {
      for (let item of config.resourceTypeList) {
        if (item.type === type) {
          return item
        }
      }
    },
    openCopyLinkPopup(link) {
      this.$refs.copyLinkPopup.open(link)
    },
  },
  data() {
    return {
      config: {
        resourceList: [],
        resourceTypeList: [],
        shareTitle: "",
        sharePoster: "",
      },
      hasReceiveResource: null, // 已经领取过的资料
    }
  },
  async onLoad(query) {
    this.hasReceiveResource = persistence.userReceiveResourceNew.get()
    try {
      const data = (
        await request({
          url: getNginxPageConfigUrl("receiveResourceNew.json"),
          method: "get",
          origin: true,
          showLoading: true,
        })
      ).data
      for (let item of data) {
        if (item.key === decodeURIComponent(query.pageType)) {
          this.config = item.config
          break
        }
      }
      // 动态设置页面标题
      uni.setNavigationBarTitle({ title: decodeURIComponent(this.config.pageTitle) })
      console.log(this.config, "this.config")
    } catch (e) {
      console.log(e, "error")
      showToast.default("网络错误，请稍后再试")
    }
  },
  onShareAppMessage() {
    return {
      title: this.config.shareTitle,
      imageUrl: getShowNginxPicUrl(this.config.sharePoster),
      path: urlUtils.getCurrentPageUrl(),
    }
  },
}
</script>

<style lang="scss" scoped></style>
